package cn.qiguai.sphapi.service.impl;

import cn.qiguai.sphapi.controller.request.CommitPayOrderParam;
import cn.qiguai.sphapi.controller.request.GetPageOrderParam;
import cn.qiguai.sphapi.controller.response.Page;
import cn.qiguai.sphapi.domain.PayOrder;
import cn.qiguai.sphapi.domain.ShoppingCartOrder;
import cn.qiguai.sphapi.mapper.AddresseeMapper;
import cn.qiguai.sphapi.mapper.PayOrderMapper;
import cn.qiguai.sphapi.mapper.ShoppingCartOrderMapper;
import cn.qiguai.sphapi.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
@Transactional
public class PayOrderServiceImpl implements PayOrderService {
	@Autowired
	private PayOrderMapper payOrderMapper;
	@Autowired
	private ShoppingCartOrderMapper shoppingCartOrderMapper;
	@Autowired
	private AddresseeMapper addresseeMapper;

	/**
	 * 30分钟将指定订单的状态改为取消
	 * @param orderId 订单id
	 */
	private void scheduleOrderExpiration(int orderId) {
		Executors.newSingleThreadScheduledExecutor().schedule(() -> {
			//查询订单支付状态
			int status = payOrderMapper.selectPayOrderStatus(orderId);

			//当订单超过30分钟未支付时，将其支付状态修改为超时
			if (status != 1) {
				payOrderMapper.updatePayOrderStatus(orderId, -1);
			}
		}, 30 * 60 * 1000, TimeUnit.MILLISECONDS);
	}

	@Override
	public boolean commitPayOrder(CommitPayOrderParam param) {
		//插入订单数据
		boolean flag1 = payOrderMapper.insertPayOrder(param) != 0 && payOrderMapper.insertPayOrderGoods(param) != 0;

		//查询购物车订单id
		List<Integer> shoppingCartOrderIds = payOrderMapper.selectShoppingCartOrderIdByPayOrderId(param.getId());

		//将购物车订单状态修改为已购买
		boolean flag2 = shoppingCartOrderMapper.updateShoppingCartOrderStatus(shoppingCartOrderIds) != 0;

		//30分钟后取消订单
		scheduleOrderExpiration(param.getId());

		return flag1 && flag2;
	}

	@Override
	public boolean pay(int orderId) {
		//修改购买订单状态为已支付
		return payOrderMapper.updatePayOrderStatus(orderId, 1) != 0;
	}

	@Override
	public Page<PayOrder> getPagePayOrder(GetPageOrderParam param) {
		//计算开始索引
		int startIndex = (param.getCurrentPage() - 1) * param.getPageSize();

		//查询总记录数
		int totalCount = payOrderMapper.selectPayOrderTotalCount(param.getUserId());

		if (totalCount == 0) {
			return new Page<>();
		}

		//查询订单基本信息
		List<PayOrder> orderList = payOrderMapper.selectPayOrderByUserId(param, startIndex);

		for (PayOrder payOrder : orderList) {
			//查询商品基本信息
			List<ShoppingCartOrder> shoppingCartOrders = payOrderMapper.selectShoppingCartOrderByPayOrderId(payOrder.getId());

			//查询商品属性
			for (ShoppingCartOrder shoppingCartOrder : shoppingCartOrders) {
				shoppingCartOrder.setSaleAttrs(shoppingCartOrderMapper.selectGoodsAttrs(shoppingCartOrder.getId()));
			}
			payOrder.setGoodsList(shoppingCartOrders);

			//查询收件人信息
			payOrder.setAddressee(addresseeMapper.selectAddresseeById(payOrder.getAddresseeId()));
		}

		Page<PayOrder> page = new Page<>();
		page.setCurrentPage(param.getCurrentPage());
		page.setPageSize(param.getPageSize());
		page.setTotalCount(totalCount);
		page.setData(orderList);
		page.computeHasNext();

		return page;
	}

	@Override
	public int getPayOrderStatus(int orderId) {
		return payOrderMapper.selectPayOrderStatus(orderId);
	}
}
